<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { statisticsApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  import { weekStarts } from '@/utils';
  import { ElMessageBox } from 'element-plus';
  import addForm from './addForm.vue';
  import rewardList from './rewardList.vue';
  import statisticsList from './statisticsList.vue';
  import Pagination from "@/components/Pagination/index.vue";
  const addFormRef = ref(),
  statisticsListRef = ref(),
  rewardListRef = ref()
  const action = reactive({
    total:0,
    list:[] as any[],
  })
  const listQuery = reactive({
    page: 1,
    limit: 20,
    week: 0,
  })
  const handleReward = (row:any = {})=>{
    rewardListRef.value.openDialog(row)
  }
  const handleDelete = (row:any = {})=>{
    ElMessageBox.confirm("是否删除此周礼物?", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
    .then(async () => {
      const loading = Toastloading();
      await statisticsApi.getGiftstarConfigDelete({
        id:row.id
      });
      loading.close();
      ToastSuccess("删除成功!");
      getList();
    })
    .catch(() => {});
  }
  const handleSerach = ()=>{
    listQuery.page = 1
    action.list = []
    getList()
  }
  const getList = async()=> {
    const loading = Toastloading()
    const res = await statisticsApi.getGiftstarConfigList(listQuery)
    loading.close()
    const { data=[], total=0 } = res.data
    action.list = data
    action.total = total
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">日期：</div>
          <el-date-picker
            v-model="listQuery.week"
            type="week"
            format="YYYY 第 ww 周"
            placeholder="选择周"
            @change="handleSerach"/>
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="primary" @click="addFormRef.openDialog()">新增</el-button>
          <el-button type="primary" @click="statisticsListRef.openDialog()">周星统计</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border>
      <el-table-column prop="id" label="ID" />
      <el-table-column prop="week" width="230" label="周" />
      <el-table-column label="礼物一">
        <el-table-column prop="gift1" label="ID" />
        <el-table-column width="100px" prop="gift1_name" label="名称" />
        <el-table-column prop="gift1_price" label="价格" />
        <el-table-column prop="rate1" label="积分倍率">
          <template #default="scope">{{ scope.row.rate1 }}%</template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="礼物二">
        <el-table-column prop="gift2" label="ID" />
        <el-table-column width="100px" prop="gift2_name" label="名称" />
        <el-table-column prop="gift2_price" label="价格" />
        <el-table-column prop="rate2" label="积分倍率">
          <template #default="scope">{{ scope.row.rate2 }}%</template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="礼物三">
        <el-table-column prop="gift3" label="ID" />
        <el-table-column width="100px" prop="gift3_name" label="名称" />
        <el-table-column prop="gift3_price" label="价格" />
        <el-table-column prop="rate3" label="积分倍率">
          <template #default="scope">{{ scope.row.rate3 }}%</template>
        </el-table-column>
      </el-table-column>
      <el-table-column prop="status_name" label="状态" />
      <el-table-column label="操作" fixed="right" width="180">
        <template #default="scope">
          <el-button type="primary" v-if="scope.row.status==2" @click="handleReward(scope.row)">奖励列表</el-button>
          <el-button type="danger" v-else @click="handleDelete(scope.row)">删除配置</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
    <addForm ref="addFormRef" @update-list="getList" />
    <rewardList ref="rewardListRef" />
    <statisticsList ref="statisticsListRef" />
  </div>
</template>